package com.wolfking.jeesite.freemarker.method;

import com.wolfking.jeesite.freemarker.FreemarkerMethod;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import lombok.extern.slf4j.Slf4j;

import java.net.URLEncoder;
import java.util.List;

@Slf4j
@FreemarkerMethod("urlEncode")
public class UrlEncodeMethod implements TemplateMethodModelEx {

    @Override
    public Object exec(List list) throws TemplateModelException {
        try {
            return URLEncoder.encode(list.get(0).toString(), "UTF-8");
        } catch (Exception e) {
            log.warn("", e);
            return list.get(0).toString();
        }
    }
}